package com.ihrm.modules.sys.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ihrm.common.utils.PageUtils;
import com.ihrm.common.utils.Query;

import com.ihrm.modules.sys.dao.DistrictDao;
import com.ihrm.modules.sys.entity.DistrictEntity;
import com.ihrm.modules.sys.service.DistrictService;


@Service("districtService")
public class DistrictServiceImpl extends ServiceImpl<DistrictDao, DistrictEntity> implements DistrictService {

    @Autowired
    private DistrictDao districtDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DistrictEntity> page = this.page(
                new Query<DistrictEntity>().getPage(params),
                new QueryWrapper<DistrictEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<DistrictEntity> level(Integer level) {
        // 1、根据层级查询所有地区
        List<DistrictEntity> districts = new LambdaQueryChainWrapper<>(districtDao)
                .le(DistrictEntity::getLevel, level)
                .list();
        // 2、递归封装层级数据
        DistrictEntity districtEntity = new DistrictEntity();
        districtEntity.setPid(-1);
        districtEntity.setId(0);
        districtEntity.setLevel(0);
        queryLevelRecursive(districtEntity, districts, level);
        return districtEntity.getChildren();
    }

    /**
     * 递归封装层级数据
     * @param vo                    上一级元素
     * @param districtEntityVoList  总集合
     * @param level                 目标层级
     */
    private void queryLevelRecursive(DistrictEntity vo, List<DistrictEntity> districtEntityVoList, int level) {
        // 1、如果是指定层级或者集合为空结束递归调用
        if (vo== null || vo.getLevel() == level || districtEntityVoList == null || districtEntityVoList.size() == 0) {
            return;
        }
        // 2、遍历查找集合中pid和目标id相同的元素
        List<DistrictEntity> children = new LinkedList<>();
        Iterator<DistrictEntity> iterator = districtEntityVoList.iterator();
        while (iterator.hasNext()) {
            DistrictEntity next = iterator.next();
            if (next.getPid().equals(vo.getId())) {
                // 2.1、找到元素，加入子集合；同时删除在原有集合中移除
                children.add(next);
                iterator.remove();
            }
        }
        if (children.size() > 0) {
            // 3、子集合不为空，递归封装层级数据
            vo.setChildren(children);
            for (DistrictEntity child: children) {
                queryLevelRecursive(child, districtEntityVoList, level);
            }
        }
    }
}
